﻿using Soft.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Soft.Service
{
    public static class LocalJobServiceSet
    {

        static LocalJobServiceSet()
        {
            LocalJobServiceSet.mRemotingCallLog = new LogFile("RemotingCall", TimeUnit.Day, 100, "Log", Encoding.Unicode);
            LocalJobServiceSet.DBConnectionGroups = new DBConnectionGroups();
        }


        /// <summary>
        /// 数据库按业务模块分组后的连接字符串集合
        /// </summary>
        public static DBConnectionGroups DBConnectionGroups { get; private set; }

  
        /// <summary>
        /// 记录远程调用地址
        /// </summary>
        /// <param name="callMethodInstruction"></param>
        /// <param name="methodCallInfo"></param>
        public static void WriteRemotingCallLog(CallMethodInstruction callMethodInstruction, MethodCallInfo methodCallInfo)
        {
            StringBuilder logMsg = new StringBuilder();
            string text = callMethodInstruction.ClientIPAddress.Equals(LocalJobServiceSet._LocalCallAddress) ? "Local internal call" : callMethodInstruction.ClientIPAddress.ToString();
            logMsg.AppendLine(string.Concat(new string[]
            {
                "Client IP address: ",
                text,
                "\tMethod name: ",
                methodCallInfo.MethodName,
                " #",
                callMethodInstruction.FunctionCode,
                "\r\nParameters:"
            }));
            if (methodCallInfo.HasParamster)
            {
                InvokeParameCollection parameItems = JsonConvertHelper.FormJson<InvokeParameCollection>(callMethodInstruction.JsonParams);
                if (parameItems == null)
                {
                    logMsg.AppendLine("\tParameters in json format cannot be converted to data of class '" + typeof(InvokeParameCollection).FullName + "'.");
                    if (string.IsNullOrWhiteSpace(callMethodInstruction.JsonParams))
                    {
                        logMsg.AppendLine("\tThe josn data is empty.");
                    }
                    else
                    {
                        logMsg.AppendLine("\tThe josn data is: '" + callMethodInstruction.JsonParams + "'.");
                    }
                }
                else
                {
                    methodCallInfo.MethodParamsterInfoList.ForEach(delegate (ParameterInfo p)
                    {
                        InvokeParameItem invokeParameItem = parameItems.Find((InvokeParameItem e) => e.Name == p.Name);
                        if (invokeParameItem != null)
                        {
                            logMsg.AppendLine(string.Concat(new string[]
                            {
                                "\t",
                                p.Name,
                                "(",
                                p.ParameterType.FullName,
                                "): ",
                                invokeParameItem.Value
                            }));
                            return;
                        }
                        if (p.HasDefaultValue)
                        {
                            logMsg.AppendLine(string.Concat(new string[]
                            {
                                "\t",
                                p.Name,
                                "(",
                                p.ParameterType.FullName,
                                "): <Used default value>",
                                p.DefaultValue.ToString()
                            }));
                            return;
                        }
                        logMsg.AppendLine(string.Concat(new string[]
                        {
                            "\t",
                            p.Name,
                            "(",
                            p.ParameterType.FullName,
                            "): <Missing input>"
                        }));
                    });
                }
            }
            else
            {
                logMsg.AppendLine("\tNo paramster.");
            }
            logMsg.AppendLine("");
            LocalJobServiceSet.mRemotingCallLog.Write(logMsg.ToString(), true, WriteFileMode.Default);
        }

   
        /// <summary>
        /// 远程调用日志文件
        /// </summary>
        private static LogFile mRemotingCallLog;


        /// <summary>
        /// 调用的IP地址
        /// </summary>
        private static IPAddress _LocalCallAddress = new IPAddress(new byte[]
        {
            byte.MaxValue,
            byte.MaxValue,
            byte.MaxValue,
            byte.MaxValue
        });


    }
}
